home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / PLOT.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  10KB  |  360 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: plot.c,v 3.26 92/03/24 22:34:34 woo Exp Locker: woo $";
  3. #endif
  4.  
  5. /* GNUPLOT - plot.c */
  6. /*
  7.  * Copyright (C) 1986, 1987, 1990, 1991, 1992   Thomas Williams, Colin Kelley
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  *
  22.  * AUTHORS
  23.  * 
  24.  *   Original Software:
  25.  *     Thomas Williams,  Colin Kelley.
  26.  * 
  27.  *   Gnuplot 2.0 additions:
  28.  *       Russell Lang, Dave Kotz, John Campbell.
  29.  *
  30.  *   Gnuplot 3.0 additions:
  31.  *       Gershon Elber and many others.
  32.  * 
  33.  * Send your comments or suggestions to 
  34.  *  info-gnuplot@ames.arc.nasa.gov.
  35.  * This is a mailing list; to join it send a note to 
  36.  *  info-gnuplot-request@ames.arc.nasa.gov.  
  37.  * Send bug reports to
  38.  *  bug-gnuplot@ames.arc.nasa.gov.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <setjmp.h>
  43. #include <signal.h>
  44. #include "plot.h"
  45. #include "setshow.h"
  46. #ifdef MSDOS
  47. #include <io.h>
  48. #endif
  49. #ifdef vms
  50. #include <unixio.h>
  51. #include <smgdef.h>
  52. extern int vms_vkid;
  53. extern smg$create_virtual_keyboard();
  54. unsigned int status[2] = {1, 0};
  55. #endif
  56. #ifdef AMIGA_LC_5_1
  57. #include <proto/dos.h>
  58. #endif
  59.  
  60. #ifdef __TURBOC__
  61. #include <graphics.h>
  62. #endif
  63.  
  64. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  65.  
  66. extern char input_line[];
  67. extern int c_token;
  68. extern FILE *outfile;
  69. extern int term;
  70.  
  71. BOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  72. BOOLEAN noinputfiles = TRUE;    /* FALSE if there are script files */
  73. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  74.  
  75. #ifndef STDOUT
  76. #define STDOUT 1
  77. #endif
  78.  
  79. jmp_buf env;
  80.  
  81. struct value *integer(),*complex();
  82.  
  83.  
  84. extern f_push(),f_pushc(),f_pushd1(),f_pushd2(),f_call(),f_call2(),f_lnot(),f_bnot(),f_uminus()
  85.     ,f_lor(),f_land(),f_bor(),f_xor(),f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  86.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  87.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  88.  
  89. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  90.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  91.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  92. #ifdef GAMMA
  93.     f_gamma(),
  94. #endif
  95.     f_floor(),f_ceil();
  96.  
  97.  
  98. struct ft_entry ft[] = {    /* built-in function table */
  99.  
  100. /* internal functions: */
  101.     {"push", f_push},    {"pushc", f_pushc},
  102.     {"pushd1", f_pushd1},    {"pushd2", f_pushd2},
  103.     {"call", f_call},    {"call2", f_call2},    {"lnot", f_lnot},
  104.     {"bnot", f_bnot},    {"uminus", f_uminus},    {"lor", f_lor},
  105.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  106.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  107.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  108.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  109.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  110.     {"power", f_power}, {"factorial", f_factorial},
  111.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  112.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  113.  
  114. /* standard functions: */
  115.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  116.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  117.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  118.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  119.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  120.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  121.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  122.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  123. #ifdef GAMMA
  124.      {"gamma", f_gamma},
  125. #endif
  126.     {"floor", f_floor},    {"ceil", f_ceil},
  127.     {NULL, NULL}
  128. };
  129.  
  130. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  131.                                     /* first in linked list */
  132. struct udvt_entry *first_udv = &udv_pi;
  133. struct udft_entry *first_udf = NULL;
  134.  
  135.  
  136.  
  137. #ifdef vms
  138.  
  139. #define HOME "sys$login:"
  140.  
  141. #else /* vms */
  142. #ifdef MSDOS
  143.  
  144. #define HOME "GNUPLOT"
  145.  
  146. #else /* MSDOS */
  147.  
  148. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  149.  
  150. #define HOME "GNUPLOT"
  151. #else /* AMIGA */
  152.  
  153. #define HOME "HOME"
  154.  
  155. #endif /* AMIGA */
  156. #endif /* MSDOS */
  157. #endif /* vms */
  158.  
  159. #ifdef unix
  160. #define PLOTRC ".gnuplot"
  161. #else /* unix */
  162. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  163. #define PLOTRC ".gnuplot"
  164. #else /* AMIGA */
  165. #define PLOTRC "gnuplot.ini"
  166. #endif /* AMIGA */
  167. #endif /* unix */
  168.  
  169. #ifdef __TURBOC__
  170. void tc_interrupt()
  171. #else
  172. #if defined( _CRAY ) || defined( sgi )
  173. void inter(an_int)
  174. int an_int;
  175. #else
  176. #ifdef NEXT
  177. void inter(int an_int)
  178. #else
  179. #ifdef sgi
  180. void inter(int sig, int code, struct sigcontext *sc)
  181. #else
  182. inter()
  183. #endif
  184. #endif
  185. #endif
  186. #endif
  187. {
  188. #ifdef MSDOS
  189. #ifdef __TURBOC__
  190.     (void) signal(SIGINT, tc_interrupt);
  191. #else
  192.     void ss_interrupt();
  193.     (void) signal(SIGINT, ss_interrupt);
  194. #endif
  195. #else  /* MSDOS */
  196.     (void) signal(SIGINT, inter);
  197. #endif  /* MSDOS */
  198.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  199.     if (term && term_init)
  200.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  201.     (void) fflush(outfile);
  202.     (void) putc('\n',stderr);
  203.     longjmp(env, TRUE);        /* return to prompt */
  204. }
  205.  
  206.  
  207. main(argc, argv)
  208.     int argc;
  209.     char **argv;
  210. {
  211. /* Register the Borland Graphics Interface drivers. If they have been */
  212. /* included by the linker.                                            */
  213.  
  214. #ifdef __TURBOC__
  215. registerfarbgidriver(EGAVGA_driver_far);
  216. registerfarbgidriver(CGA_driver_far);
  217. registerfarbgidriver(Herc_driver_far);
  218. registerfarbgidriver(ATT_driver_far);
  219. #endif
  220. #ifdef X11
  221.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  222. #endif 
  223.  
  224. #ifdef apollo
  225.     apollo_pfm_catch();
  226. #endif
  227.  
  228.     setbuf(stderr,(char *)NULL);
  229. #ifdef UNIX
  230.     setlinebuf(stdout);
  231. #endif
  232.     outfile = stdout;
  233.     (void) complex(&udv_pi.udv_value, Pi, 0.0);
  234.  
  235.      interactive = FALSE;
  236.      init_terminal();        /* can set term type if it likes */
  237.  
  238. #ifdef AMIGA_LC_5_1
  239.      if (IsInteractive(Input()) == DOSTRUE) interactive = TRUE;
  240.      else interactive = FALSE;
  241. #else
  242.      interactive = isatty(fileno(stdin));
  243. #endif
  244.      if (argc > 1)
  245.       interactive = noinputfiles = FALSE;
  246.      else
  247.       noinputfiles = TRUE;
  248.  
  249.      if (interactive)
  250.       show_version();
  251. #ifdef vms   /* initialise screen management routines for command recall */
  252.           if (status[1] = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL)
  253.                done(status[1]);
  254. #endif
  255.  
  256.     if (!setjmp(env)) {
  257.         /* first time */
  258.         interrupt_setup();
  259.         load_rcfile();
  260.  
  261.         if (interactive && term != 0)    /* not unknown */
  262.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  263.                 term_tbl[term].name);
  264.     } else {    
  265.         /* come back here from int_error() */
  266.         load_file_error();    /* if we were in load_file(), cleanup */
  267. #ifdef vms
  268.         /* after catching interrupt */
  269.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  270.           so reopen stdout. */
  271.         if (outfile == stdout) {
  272.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  273.               /* couldn't reopen it so try opening it instead */
  274.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  275.                  /* don't use int_error here - causes infinite loop! */
  276.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  277.               }
  278.            }
  279.            outfile = stdout;
  280.         }
  281. #endif                    /* VMS */
  282.         if (!interactive && !noinputfiles)
  283.          done(IO_ERROR);            /* exit on non-interactive error */
  284.     }
  285.  
  286.      if (argc > 1) {
  287.         /* load filenames given as arguments */
  288.         while (--argc > 0) {
  289.            ++argv;
  290.            c_token = NO_CARET; /* in case of file not found */
  291.            load_file(fopen(*argv,"r"), *argv);    
  292.         }
  293.     } else {
  294.         /* take commands from stdin */
  295.         while(TRUE)
  296.          com_line();
  297.     }
  298.  
  299.      done(IO_SUCCESS);
  300. }
  301.  
  302. /* Set up to catch interrupts */
  303. interrupt_setup()
  304. {
  305. #ifdef MSDOS
  306. #ifdef __TURBOC__
  307.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  308. #else
  309.         void ss_interrupt();
  310.         save_stack();                /* work-around for MSC 4.0/MSDOS 3.x bug */
  311.         (void) signal(SIGINT, ss_interrupt);
  312. #endif
  313. #else /* MSDOS */
  314.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  315. #endif /* MSDOS */
  316. }
  317.  
  318.  
  319. /* Look for a gnuplot start-up file */
  320. load_rcfile()
  321. {
  322.     register FILE *plotrc;
  323.     static char home[80];
  324.     static char rcfile[sizeof(PLOTRC)+80];
  325.  
  326.     /* Look for a gnuplot init file in . or home directory */
  327. #ifdef vms
  328.     (void) strcpy(home,HOME);
  329. #else /* vms */
  330. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  331.     strcpy(home,getenv(HOME));
  332.     {
  333.         int h;
  334.         h = strlen(home) - 1;
  335.         if (h >= 0) {
  336.             if ((home[h] != ':') && (home[h] != '/')) {
  337.                 home[h] = '/';
  338.                 home[h+1] = '\0';
  339.             }
  340.            }
  341.     }
  342. #else /* AMIGA */
  343.     (void) strcat(strcpy(home,getenv(HOME)),"/");
  344. #endif /* AMIGA */
  345. #endif /* vms */
  346. #ifdef NOCWDRC
  347.     /* inhibit check of init file in current directory for security reasons */
  348.     {
  349. #else
  350.     (void) strcpy(rcfile, PLOTRC);
  351.     plotrc = fopen(rcfile,"r");
  352.     if (plotrc == (FILE *)NULL) {
  353. #endif
  354.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  355.        plotrc = fopen(rcfile,"r");
  356.     }
  357.     if (plotrc)
  358.      load_file(plotrc, rcfile);
  359. }
  360.